﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.Progect
{
   public class T_PUB_ATTRIBUTE
    {
        public T_PUB_ATTRIBUTE()
        { }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Progect.T_PUB_ATTRIBUTE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_ATTRIBUTE(");
            strSql.Append("ATTRIBUTE_ID,ATTRIBUTE_NAME,ATTRIBUTE_TYPE,REMARK)");

            strSql.Append(" values (");
            strSql.Append("@ATTRIBUTE_ID,@ATTRIBUTE_NAME,@ATTRIBUTE_TYPE,@REMARK)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ATTRIBUTE_ID", DbType.Int32, model.ATTRIBUTE_ID);
            db.AddInParameter(dbCommand, "ATTRIBUTE_NAME", DbType.String, model.ATTRIBUTE_NAME);
            db.AddInParameter(dbCommand, "ATTRIBUTE_TYPE", DbType.Int32, model.ATTRIBUTE_TYPE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Progect.T_PUB_ATTRIBUTE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_ATTRIBUTE set ");
            strSql.Append("ATTRIBUTE_NAME=@ATTRIBUTE_NAME,");
            strSql.Append("ATTRIBUTE_TYPE=@ATTRIBUTE_TYPE,");
            strSql.Append("REMARK=@REMARK");
            strSql.Append(" where ATTRIBUTE_ID=@ATTRIBUTE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ATTRIBUTE_ID", DbType.Int32, model.ATTRIBUTE_ID);
            db.AddInParameter(dbCommand, "ATTRIBUTE_NAME", DbType.String, model.ATTRIBUTE_NAME);
            db.AddInParameter(dbCommand, "ATTRIBUTE_TYPE", DbType.Int32, model.ATTRIBUTE_TYPE);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ATTRIBUTE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_ATTRIBUTE ");
            strSql.Append(" where ATTRIBUTE_ID=@ATTRIBUTE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ATTRIBUTE_ID", DbType.Int32, ATTRIBUTE_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Progect.T_PUB_ATTRIBUTE GetModel(int ATTRIBUTE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ATTRIBUTE_ID,ATTRIBUTE_NAME,ATTRIBUTE_TYPE,REMARK from T_PUB_ATTRIBUTE ");
            strSql.Append(" where ATTRIBUTE_ID=@ATTRIBUTE_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ATTRIBUTE_ID", DbType.Int32, ATTRIBUTE_ID);
            Model.Progect.T_PUB_ATTRIBUTE model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ATTRIBUTE_ID,ATTRIBUTE_NAME,ATTRIBUTE_TYPE,REMARK ");
            strSql.Append(" FROM T_PUB_ATTRIBUTE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_ATTRIBUTE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ATTRIBUTE_ID,ATTRIBUTE_NAME,ATTRIBUTE_TYPE,REMARK ");
            strSql.Append(" FROM T_PUB_ATTRIBUTE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Progect.T_PUB_ATTRIBUTE> list = new List<Model.Progect.T_PUB_ATTRIBUTE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Progect.T_PUB_ATTRIBUTE ReaderBind(IDataReader dataReader)
        {
            Model.Progect.T_PUB_ATTRIBUTE model = new Model.Progect.T_PUB_ATTRIBUTE();
            object ojb;
            ojb = dataReader["ATTRIBUTE_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ATTRIBUTE_ID = (int)ojb;
            }
            model.ATTRIBUTE_NAME = dataReader["ATTRIBUTE_NAME"].ToString();
            ojb = dataReader["ATTRIBUTE_TYPE"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ATTRIBUTE_TYPE = (int)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            return model;
        }

        #endregion  Method
    }
}
